package ca.on.eho.qa.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.*;
import org.testng.Assert;

import ca.on.eho.qa.util.Constants;

/**
 * @author larissa.smirnova
 * 
 * Created on: Oct-16-2014
 * Change log:
 * 
 */

public class CSPortlet extends Page {
	
	private static WebElement element = null;
	private static WebDriverWait wait = null;
	
	public final String	HN_INVALID = "Health Number is invalid. Please verify and resubmit.";
	public final String	HN_REQUIRED = "A Health Number is required. Please enter and resubmit.";
	public final String	DOB_INVALID = "Date of Birth is invalid. Please verify and resubmit.";
	public final String	DOB_REQUIRED = "A Date of Birth is required. Please enter and resubmit.";
	public final String	GENDER_REQUIRED = "A Gender is required. Please select and resubmit.";
	public final String	NO_PATIENT_FOUND = "No Patient was found matching specified attributes. Please verify and resubmit.";

	public By by_healthNumberField = By.cssSelector("input.healthCardNumber");
	public By by_dobField = By.cssSelector("input.clientSelectorDate");
	public By by_genderDropdown = By.cssSelector("select.clientSelectorGender");
	public By by_submitButton = By.xpath("//div[@id='clientSelectorPortlet_gwt-uid-1']/div/div[2]/table/tbody/tr[7]/td[2]/table/tbody/tr/td/div");
	public By by_clearButton = By.id("clientSelectorClearButton");
	public By by_confirmClientButton = By.cssSelector("div.button-toolbar > button.gwt-Button");
	
	public By by_hnError = By.xpath(".//*[@id='clientSelectorPortlet_gwt-uid-1']/div/div[2]/table/tbody/tr[2]/td[2]/div");	
	public By by_dobError = By.xpath(".//*[@id='clientSelectorPortlet_gwt-uid-1']/div/div[2]/table/tbody/tr[4]/td[2]/div");
	public By by_genderError = By.xpath(".//*[@id='clientSelectorPortlet_gwt-uid-1']/div/div[2]/table/tbody/tr[6]/td[2]/div");
	public By by_noPatientFoundError = By.xpath(".//*[@id='clientSelectorPortlet_gwt-uid-1']/div/div[2]/table/tbody/tr[8]/td[2]/div");
	
	public By by_csPortlet = By.xpath("//div[@id='clientSelectorPortlet_gwt-uid-1']");
	
	
	public CSPortlet(PageManager pages) {
		super(pages);	
	}
	
	@FindBy(css = "input.healthCardNumber")
	private WebElement healthNumberField;
	
	@FindBy(css = "input.clientSelectorDate")
	private WebElement dobField;
	
	@FindBy(xpath = "//div[@id='clientSelectorPortlet_gwt-uid-1']/div/div[2]/table/tbody/tr[7]/td[2]/table/tbody/tr/td/div")
	private WebElement submitButton;
	
	@FindBy(id = "clientSelectorClearButton")
	private WebElement clearButton;
	
	@FindBy(css = "div.button-toolbar > button.gwt-Button")
	private WebElement confirmClientButton;

	@FindBy(xpath = ".//*[@id='clientSelectorPortlet_gwt-uid-1']/div/div[2]/table/tbody/tr[2]/td[2]/div")
	private WebElement hnError;
	
	@FindBy(xpath = ".//*[@id='clientSelectorPortlet_gwt-uid-1']/div/div[2]/table/tbody/tr[4]/td[2]/div")
	private WebElement dobError;
	
	@FindBy(xpath = ".//*[@id='clientSelectorPortlet_gwt-uid-1']/div/div[2]/table/tbody/tr[6]/td[2]/div")
	private WebElement genderError;
	
	@FindBy(xpath = ".//*[@id='clientSelectorPortlet_gwt-uid-1']/div/div[2]/table/tbody/tr[8]/td[2]/div")
	private WebElement noPatientFoundError;
	
	@FindBy(xpath = "")
	private WebElement infoLabel;
	
	@FindBy(xpath = "")
	private WebElement ageLabel;
	
	private Select genderDropdown() {
		return new Select(driver.findElement(by_genderDropdown));
	
	}
	
	
	public CSPortlet ensurePageLoaded() {
		super.ensurePageLoaded();
		wait.until(ExpectedConditions.presenceOfElementLocated(by_healthNumberField));
		return this;		
	}	
	
	public CSPortlet setHealthNumberField(String text) {
		healthNumberField.clear();	
		healthNumberField.sendKeys(text);	
		return this;		
	}
	
	public CSPortlet setDOBField(String text) {
		dobField.clear();
		dobField.sendKeys(text);	
		return this;		
	}
	
	public CSPortlet setGenderDropdown(String text) {
		this.genderDropdown().selectByVisibleText(text);
		return this;		
	}
	
	public CSPortlet clickSubmitButton() {
		submitButton.sendKeys(Keys.ENTER);	
		return this;		
	}
		
	public CSPortlet clickClearButton() {
		clearButton.sendKeys(Keys.ENTER);	
		return this;		
	}
	
	public CSPortlet clickConfirmClientButton() {
		confirmClientButton.click();	
		return this;		
	}
	
	public String getHealthNumberField() {
		return healthNumberField.getAttribute("value");	
	}	

	public String getDOBField() {
		return dobField.getAttribute("value");	
	}	
	
	public String getGenderDropdown() {
		return "";	
	}
	
	public String getHNError() {
		return hnError.getText();	
	}

	public String getDOBError() {
		return dobError.getText();	
	}
	
	public String getGenderError() {
		return genderError.getText();	
	}
	
	public String getNoPatientFound() {
		return noPatientFoundError.getText();	
	}
	
	public String getAge() {
		return genderError.getText();	
	}
	
	public String getInfo() {
		return noPatientFoundError.getText();		
	}


}
